package com.xuan.shop.service.impl;

import com.xuan.shop.dao.ICarDao;
import com.xuan.shop.entity.Car;
import com.xuan.shop.service.ICarService;
import com.xuan.shop.vo.CarTypeVo;
import jdk.nashorn.internal.ir.LiteralNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CarServiceImpl implements ICarService {
    @Autowired
    private ICarDao dao;
    @Override
    public void save(Car car) {
        dao.save(car);
    }

    @Override
    public void deleteById(int id) {
        dao.deleteById(id);
    }

    @Override
    public void update(Car car) {
        dao.save(car);
    }

    @Override
    public List<Car> findAll() {
        return dao.findAll();
    }

    @Override
    public List<Car> findAllByBrand(Integer brand) {
        return dao.findAllByBrand(brand);
    }

    @Override
    public List<Car> findAllByCommand() {
        return null;
    }

    @Override
    public List<CarTypeVo> countType() {
        Object[] objects = dao.countCarByType();

        List<CarTypeVo> ctv=new ArrayList<>();
        for (int i = 0; i < objects.length; i++) {
            Object[] o= (Object[]) objects[i];
            ctv.add(new CarTypeVo(o[0],o[1]));
        }
        System.out.println(ctv);
        return ctv;
    }


}
